<template>
  <div id="goods_app">
    <div class="control_button">
      <el-button type="primary" round plain @click="isShow.flag = true"
        >新增</el-button
      >
    </div>
    <el-table :data="goodsList" border style="width: 100%">
      <el-table-column fixed prop="id" label="ID" width="100">
      </el-table-column>
      <el-table-column prop="firstcatename" label="一级分类" width="150">
      </el-table-column>
      <el-table-column prop="secondcatename" label="二级分类" width="150">
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" width="150">
      </el-table-column>
      <el-table-column prop="img" label="商品图片" width="150">
        <template v-slot="$props">
          <img :src="$url + $props.row.img" alt="" width="50px" />
        </template>
      </el-table-column>
      <el-table-column prop="price" label="零售价格" width="150">
        <template v-slot="$props"> &yen; {{ $props.row.price }} </template>
      </el-table-column>
      <el-table-column prop="market_price" label="市场价格" width="150">
        <template v-slot="$props">
          &yen; {{ $props.row.market_price }}
        </template>
      </el-table-column>
      <el-table-column prop="isnew" label="是否新品" width="150">
        <template v-slot="$props">
          <el-switch
            v-model="$props.row.isnew"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="ishot" label="是否热卖" width="150">
        <template v-slot="$props">
          <el-switch
            v-model="$props.row.ishot"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150">
        <template v-slot="$props">
          <el-button
            type="warning"
            size="mini"
            round
            plain
            v-if="$props.row.status == 1"
            >正常</el-button
          >
          <el-button type="danger" size="mini" round plain v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template v-slot="$props">
          <el-button
            type="warning"
            size="mini"
            round
            plain
            @click="edit_goods($props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            round
            plain
            @click="del_goods($props.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      :page-size="pagintor.size"
      background
      layout="prev, pager, next, sizes"
      :page-sizes="[5, 10, 15]"
      :total="count"
      @current-change="changePage"
    >
    </el-pagination>
    <Dialog :isShow="isShow" @reload="reload" ref="dialog_ref" />
  </div>
</template>

<script>
import Dialog from "../components/addGoods.vue";
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      pagintor: { page: 1, size: 5 },
      isShow: { flag: false },
    };
  },
  computed: {
    ...mapState("goods_model", ["goodsList", "count"]),
  },
  methods: {
    edit_goods(obj) {
      this.isShow.flag = true;
      //设置数据
      this.$refs.dialog_ref.setDate(obj);
    },
    del_goods(id) {
      this.$axios.post("/goodsdelete", { id }).then((res) => {
        if (res.code == 200) {
          this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消删除",
              });
            });
          this.reload();
        }
      });
    },
    reload() {
      this.goodsCountActions();
      this.goodsListActions(this.pagintor);
    },
    changePage(page) {
      this.pagintor.page = page;
      this.goodsCountActions();
      this.goodsListActions(this.pagintor);
    },
    handleSizeChange(val) {
      this.pagintor.size = val;
      this.goodsCountActions();
      this.goodsListActions(this.pagintor);
    },
    ...mapActions("goods_model", ["goodsCountActions", "goodsListActions"]),
  },
  created() {
    this.goodsCountActions();
    this.goodsListActions(this.pagintor);
  },
  components: {
    Dialog,
  },
};
</script>

<style>

</style>